/*************************************************************************
	> File Name: client_recver.c
	> Author: 
	> Mail: 
	> Created Time: 2020年06月13日 星期六 14时13分03秒
 ************************************************************************/

#include "head.h"
#include "game.h"
#include "client_re_drew.h"

extern int sockfd;
void *client_recv(void *arg) {
	while (1) {
		struct FootBallMsg msg;
		struct User user;
		bzero(&msg, sizeof(msg));
		recv(sockfd, (void *)&msg, sizeof(msg), 0);
		strcpy(user.name, msg.name);
		user.team = msg.team;
		if (msg.type & FT_TEST) {//接收服务端心跳测试
			DBG(RED"HeartBeat from Server 心跳\n"NONE);
			msg.type = FT_ACK;//响应服务端还活着
			send(sockfd, (void *)&msg, sizeof(msg), 0);
		} else if (msg.type & FT_MSG) {//接收服务端的信息
			DBG(GREEN"%s : "NONE"%s\n", user.name, msg.msg);
			Show_Message( , &user, msg.msg, 0);
		} else if (msg.type & FT_WALL) {//收到服务端广播的信息
			DBG(GREEN"Server Msg: "NONE"%s\n", msg.msg);
			Show_Message( , NULL, msg.msg, 1);
		} else if (msg.type & FT_FIN) {//收到服务端下线通知
			DBG(GREEN"Server is going to stop!\n"NONE);
			endwin();	
			exit(0);
		} else if (msg.type & FT_GAME) {//服务端向客户端广播的json实时地图
			DBG(YELLOW"JSON:"NONE"%s", msg.msg);
			cJSON *root = cJSON_Parse(msg.msg);
			re_drew(root);
		} else {
			DBG(GREEN"Server Msg :"NONE"Unsupport Message Type.\n");
		}
	}
}
/*
{
	"name":	"test",
	"ball":	{
		"x":	57,
		"y":	12
	},
	"score":	{
		"bscore":	0,
		"rscore":	0
	},
	"users":	[{
			"team":	0,
			"name":	"ZFB",
			"locx":	2,
			"locy":	12
		}]
}
*/